package com.beiing.tianshuai.tianshuai.mine.model;

import com.beiing.tianshuai.tianshuai.entity.CommonBean;
import com.beiing.tianshuai.tianshuai.entity.MyOrderBean;
import com.beiing.tianshuai.tianshuai.mine.entity.OrderDeleteBean;
import com.beiing.tianshuai.tianshuai.mine.entity.OrderExtendTransportBean;
import com.beiing.tianshuai.tianshuai.network.DataManager;
import com.beiing.tianshuai.tianshuai.util.LogUtils;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * 订单列表页Model
 *
 * @author Jinni
 * @date 2017/7/27 0027
 */

public class MyOrderModel implements MyOrderModelImpl {

    private static final String TAG = "MyOrderModel";
    private MyOrderModel.OnRequestListener mListener;

    public MyOrderModel(OnRequestListener listener) {
        mListener = listener;
    }

    @Override
    public void getMyOrderInfoResult(String uid, int type, String lastId, int pageSize) {
        Observable<MyOrderBean> observable = DataManager.getMyOrderInfoResult(
                uid,
                type,
                lastId,
                pageSize
        );
        Observable<MyOrderBean> subscribeOn = observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        subscribeOn.subscribe(new DisposableObserver<MyOrderBean>() {

            @Override
            public void onNext(@NonNull MyOrderBean bean) {
                if (mListener != null) {
                    mListener.onSuccess(bean);
                }
            }

            @Override
            public void onError(@NonNull Throwable e) {
                if (mListener != null) {
                    mListener.onError(e);
                }
            }

            @Override
            public void onComplete() {
                LogUtils.i("------------------ onCompleted ------------------", TAG);
            }
        });
    }

    @Override
    public void getCancelRequestResult(String oid) {
        Observable<CommonBean> observable = DataManager.getCancelRequestResult(
                oid
        );
        Observable<CommonBean> subscribeOn = observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        subscribeOn.subscribe(new DisposableObserver<CommonBean>() {

            @Override
            public void onNext(@NonNull CommonBean bean) {
                if (mListener != null) {
                    mListener.onCancelSuccess(bean);
                }
            }

            @Override
            public void onError(@NonNull Throwable e) {
                if (mListener != null) {
                    mListener.onError(e);
                }
            }

            @Override
            public void onComplete() {
                LogUtils.i("------------------ onCompleted ------------------", TAG);
            }
        });
    }

    @Override
    public void getDelRequestResult(String oid, final int position) {
        Observable<OrderDeleteBean> observable = DataManager.getDelRequestResult(
                oid
        );
        Observable<OrderDeleteBean> subscribeOn = observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        subscribeOn.subscribe(new DisposableObserver<OrderDeleteBean>() {

            @Override
            public void onNext(@NonNull OrderDeleteBean bean) {
                if (mListener != null) {
                    mListener.onDelSuccess(bean, position);
                }
            }

            @Override
            public void onError(@NonNull Throwable e) {
                if (mListener != null) {
                    mListener.onError(e);
                }
            }

            @Override
            public void onComplete() {
                LogUtils.i("------------------ onCompleted ------------------", TAG);
            }
        });
    }

    @Override
    public void getDelayInReceivingResult(String orderId, final MyOrderBean.DataBean.ResultBean orderBean) {
        RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
                .addFormDataPart("ordersid", orderId)
                .build();
        Observable<OrderExtendTransportBean> observable = DataManager.extentOrderRequest(
                body
        );
        Observable<OrderExtendTransportBean> subscribeOn = observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        subscribeOn.subscribe(new DisposableObserver<OrderExtendTransportBean>() {

            @Override
            public void onNext(@NonNull OrderExtendTransportBean bean) {
                if (mListener != null) {
                    mListener.onRequestDelayInReceivingSuccess(bean, orderBean);
                }
            }

            @Override
            public void onError(@NonNull Throwable e) {
                if (mListener != null) {
                    mListener.onRequestDelayInReceivingError(e);
                }
            }

            @Override
            public void onComplete() {
                LogUtils.i("------------------ onCompleted ------------------", TAG);
            }
        });
    }

    public interface OnRequestListener {

        /**
         * 订单列表请求结果
         *
         * @param bean 请求结果
         */
        void onSuccess(MyOrderBean bean);

        /**
         * 取消订单请求结果
         *
         * @param bean 请求结果
         */
        void onCancelSuccess(CommonBean bean);

        /**
         * 删除订单请求结果
         *
         * @param bean     请求结果
         * @param position 用户所操作订单所在位置
         */
        void onDelSuccess(OrderDeleteBean bean, int position);

        /**
         * 操作失败
         *
         * @param throwable 请求异常信息
         */
        void onError(Throwable throwable);

        /**
         * 订单延期请求成功
         *
         * @param bean      请求结果
         * @param orderBean 订单数据实体类
         */
        void onRequestDelayInReceivingSuccess(OrderExtendTransportBean bean,
                                              MyOrderBean.DataBean.ResultBean orderBean);

        /**
         * 订单延期请求失败
         *
         * @param throwable 请求异常信息
         */
        void onRequestDelayInReceivingError(Throwable throwable);
    }
}
